package com.hencoderplus.demo1;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

/**
 * hencoderplus
 *
 * @author lzw
 * @date 2018/10/23
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class AvatarView extends View {

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap bitmap;
    public static final float PADDING =  Utils.dp2Px(10);
    public static final float WIDTH =  Utils.dp2Px(100);
    public static final int EDGE = (int) Utils.dp2Px(5);
    private RectF savedArea = new RectF();

    {
        bitmap = Utils.decodeBitmap(getContext(), (int) WIDTH);
    }

    public AvatarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        savedArea.set(PADDING,PADDING,PADDING+WIDTH,PADDING+WIDTH);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawOval(PADDING,PADDING,PADDING+WIDTH,PADDING+WIDTH,paint);
        int saved = canvas.saveLayer(savedArea,paint);
        canvas.drawOval(PADDING+EDGE,PADDING+EDGE,PADDING+WIDTH-EDGE,PADDING+ WIDTH-EDGE,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap,PADDING,PADDING,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(saved);
    }
}
